package com.soilflow.system.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soilflow.common.constant.UserConstants;
import com.soilflow.common.core.domain.entity.SysUserReception;
import com.soilflow.common.domain.DahuBusiness;
import com.soilflow.common.domain.DahuProduct;
import com.soilflow.common.domain.dto.DahuBusinessDto;
import com.soilflow.common.utils.StringUtils;
import com.soilflow.system.mapper.DahuBusinessMapper;
import com.soilflow.system.service.IDahuBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author party
 * @date 2023-10-26
 */
@Service
public class DahuBusinessServiceImpl extends ServiceImpl<DahuBusinessMapper, DahuBusiness> implements IDahuBusinessService {
    @Resource
    private DahuBusinessMapper dahuBusinessMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
//    @Autowired
//    private IDahuProductService dahuProductService;
    @Resource
    private DahuBusinessMapper businessMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public DahuBusiness selectDahuBusinessById(Long id)
    {
        return dahuBusinessMapper.selectDahuBusinessById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param dahuBusiness 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<DahuBusiness> selectDahuBusinessList(DahuBusiness dahuBusiness)
    {
        return dahuBusinessMapper.selectDahuBusinessList(dahuBusiness);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param dahuBusiness 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertDahuBusiness(DahuBusiness dahuBusiness) {
        return dahuBusinessMapper.insertDahuBusiness(dahuBusiness);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param dahuBusiness 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateDahuBusiness(DahuBusiness dahuBusiness)
    {
        return dahuBusinessMapper.updateDahuBusiness(dahuBusiness);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteDahuBusinessByIds(Long[] ids)
    {
        return dahuBusinessMapper.deleteDahuBusinessByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteDahuBusinessById(Long id)
    {
        return dahuBusinessMapper.deleteDahuBusinessById(id);
    }

    @Override
    public Map updateStatus(Map params) {
        //获取用户id
        String id = params.get("id").toString();
        String key = "business_"+id;
        //构建失效时间
        Integer freezingTime = Integer.valueOf((Integer) params.get("freezingTime"));  //冻结三天 或者7天
        int day=0;
        if (freezingTime == 1) {
            day=3;
        } else if (freezingTime == 2) {
            day=7;
        }
        //将数据存入redis中
        String vaule = JSON.toJSONString(params);
        if (day>0) {
            redisTemplate.opsForValue().set(key,vaule,day, TimeUnit.MINUTES);
        }else {
            redisTemplate.opsForValue().set(key,vaule);
        }
        Map retMap=new HashMap();

        LambdaUpdateWrapper<DahuBusiness> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(DahuBusiness::getId,id);
        wrapper.set(DahuBusiness::getStatus,1);
        boolean update = this.update(wrapper);
        //冻结商家后，还需将该商家上架的商品进行下架
        LambdaUpdateWrapper<DahuProduct> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(DahuProduct::getBusinessId,id);
        updateWrapper.set(DahuProduct::getDeleted,1);
//        dahuProductService.update(updateWrapper);
        retMap.put("message","冻结成功");
        return retMap;
    }

    @Override
    public Map thaw(Map params) {
        String id = params.get("id").toString();
        String key = "business_"+id;
        //删除redis数据
        redisTemplate.delete(key);
        Map retMap=new HashMap();

        LambdaUpdateWrapper<DahuBusiness> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(DahuBusiness::getId,id);
        wrapper.set(DahuBusiness::getStatus,0);
        boolean update = this.update(wrapper);
        //解冻商家后，还需将该商家上架的商品进行上架
        LambdaUpdateWrapper<DahuProduct> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(DahuProduct::getBusinessId,id);
        updateWrapper.set(DahuProduct::getDeleted,0);
//        dahuProductService.update(updateWrapper);
        retMap.put("message","解冻成功");
        return retMap;
    }

    /**
     * 商家入驻
     * @param dahuBusiness
     * @return
     */
    @Override
    public boolean settleIn(DahuBusiness dahuBusiness) {
//        dahuBusiness.setToExamine(1);
        return this.save(dahuBusiness);
    }

    @Override
    public DahuBusiness selectUserByUserName(String username) {
        LambdaQueryWrapper<DahuBusiness> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(DahuBusiness::getName,username);
        DahuBusiness business = this.getOne(wrapper);
        return business;
    }

    @Override
    public String checkUserNameUnique(String name) {
        int count = businessMapper.checkUserNameUnique(name);
        if (count > 0)
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public String checkPhoneUnique(DahuBusinessDto business) {
        Long userId = StringUtils.isNull(business.getId()) ? -1L : business.getId();
        DahuBusiness info = businessMapper.checkPhoneUnique(business.getPhone());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != userId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public DahuBusiness getPhonenumber(String phone) {
        LambdaQueryWrapper<DahuBusiness> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(DahuBusiness::getPhone,phone);
        DahuBusiness business = this.getOne(wrapper);
        return business;
    }

    @Override
    public int toexamineupdate(DahuBusiness dahuBusiness) {
        dahuBusiness.setToExamine(2);
        dahuBusiness.setBecome(1);
        this.updateById(dahuBusiness);
        return 1;
    }

}
